/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.school.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.DateUtils;
import cn.apestar.common.utils.JedisUtils;
import cn.apestar.common.utils.excel.ExportExcel;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.school.entity.Deposit;
import cn.apestar.modules.common.school.init.BaiduMap;
import cn.apestar.modules.common.school.service.DepositService;
import cn.apestar.modules.sys.entity.Dict;

/**
 * 学校Controller
 * @author LVH
 * @version 2016-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/fetch")
public class FetchController extends BaseController {

    @Autowired
    private DepositService depositService;

    @RequestMapping(value = {"list" , ""})
    public String list(Deposit school, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Deposit> page = depositService.findPage(new Page<Deposit>(request, response), school);
        model.addAttribute("page", page);
        model.addAttribute("learningClassroom", school);
        return "modules/fetch/fetchList";
    }

    @ResponseBody
    @RequestMapping(value = "initFetch")
    public Object initFetch(Dict dict, RedirectAttributes redirectAttributes) {

        BaiduMap baiduMap = new BaiduMap();
        if (JedisUtils.exists(baiduMap.cacheKey)) {
            return getSuccessMessage("已经在抓取数据", null);
        } else {
            dict.setLabel(dict.getCode() + dict.getType());
            baiduMap.setCity(dict.getType());
            baiduMap.setType(dict.getCode());
            baiduMap.run();
        }

        return getDefaultSuccessMessage();
    }

    /**
     * 导出用户数据
     * @param user
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(Deposit school, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {

        try {
            String fileName = "提现记录" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<Deposit> page = depositService.findPage(new Page<Deposit>(request, response), school);
            new ExportExcel("提现记录", Deposit.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出用户失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + adminPath + "/sys/user/list?repage";
    }

}